﻿#region Header

// Majesty of Omega - The next evolution of macro management space strategy
// 
// Copyright 2007-2009 André Claaßen
// 
// This file is protected by the terms and conditions of the
// Microsoft Community License (Ms-CL 1.1), a copy of which should
// have been distributed along with this software. If not,
// you may find the license information at the following URL:
// 
// http://www.microsoft.com/resources/sharedsource/licensingbasics/communitylicense.mspx

#endregion

using System.IO;
using Majesty_of_Omega.Util;
#if NUNIT
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using ClassInitialize = NUnit.Framework.TestFixtureSetUpAttribute;
using ClassCleanup = NUnit.Framework.TestFixtureTearDownAttribute;
#elif MBUNIT
using MbUnit.Framework;
using TestClass = MbUnit.Framework.TestFixtureAttribute;
using TestMethod = MbUnit.Framework.TestAttribute;
using TestInitialize = MbUnit.Framework.SetUpAttribute;
using TestCleanup = MbUnit.Framework.TearDownAttribute;
using ClassInitialize = MbUnit.Framework.TestFixtureSetUpAttribute;
using ClassCleanup = MbUnit.Framework.TestFixtureTearDownAttribute;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif


namespace Test.Majesty_of_Omega.TestUtils
{
    /// <summary>
    /// This class helps tests for serializing and deserializing
    /// </summary>
    internal static class SerializeHelper
    {
        /// <summary>
        /// Saves a object graph, loads it and compare it.
        /// </summary>
        /// <param name="graph">The object graph.</param>
        /// <param name="assertMessage">assert message</param>
        /// <returns>Returns the loaded object graph</returns>
        public static object SaveLoadAndCompare(object graph, string assertMessage)
        {
            string tempFilename = GetTempFilename();
            SerializerHelper.SaveBinary(graph, tempFilename);
            object graphLoaded = SerializerHelper.LoadBinary(tempFilename);
            Assert.AreEqual(graph, graphLoaded, assertMessage);
            File.Delete(tempFilename);
            return graphLoaded;
        }

        /// <summary>
        /// Gets the temp filename for saved files.
        /// </summary>
        /// <returns></returns>
        private static string GetTempFilename()
        {
            return Path.GetTempFileName() + ".sav";
        }
    }
}